package com.mls.fivedistrict.server.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.mls.fivedistrict.dao.NewsReadReportMapper;
import com.mls.fivedistrict.entity.request.AwardRequest;
import com.mls.fivedistrict.enums.RecordProductTypeEnum;
import com.mls.fivedistrict.exception.CustomerException;
import com.mls.fivedistrict.pojo.News;
import com.mls.fivedistrict.pojo.NewsReadReport;
import com.mls.fivedistrict.pojo.NewsReadReport;
import  com.mls.fivedistrict.pojo.NewsReadReport;

import com.mls.fivedistrict.server.AwardService;
import com.mls.fivedistrict.server.NewsService;
import com.mls.fivedistrict.server.NewsReadReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * <p>
 * 新闻阅读报表 服务实现类
 * </p>
 *
 * @author Five Team
 * @since 2018-05-15
 */
@Service
public class NewsReadReportServiceImpl extends ServiceImpl<NewsReadReportMapper, NewsReadReport> implements NewsReadReportService {

    @Autowired
    NewsService newsService;

    @Autowired
    AwardService awardService;
    @Transactional
    @Override
    public boolean addNewsReadReport(NewsReadReport newsReadReport) {
        boolean insert = insert(newsReadReport);
        if (insert) {
            News news = newsService.selectById(newsReadReport.getNewsId());
            if (news == null) {
                throw new CustomerException("新闻不存在");
            }
            if (news.getRewardDwqNumber().compareTo(new BigDecimal(0)) > 0) {
                AwardRequest awardRequest = new AwardRequest();
                awardRequest.setTargetUserId(newsReadReport.getUserId());
                awardRequest.setIntegralId(18L);
                awardRequest.setAwardNumber(news.getRewardDwqNumber());
                awardRequest.setSource(RecordProductTypeEnum.CONTRIBUTION.getValue());
                awardRequest.setReason("阅读新闻奖励");
                awardService.awardDwq(awardRequest);
            }

        }
        return insert;
    }
}
